<?php
/**
 * Jishupu-Cms
 * @Author:技术铺
 * @Date:2021/11/22
 */

namespace App\Services;

use Illuminate\Support\Facades\Cache;

/**
 * 统一缓存服务层：添加缓存、获取缓存、删除指定缓存、清空所有缓存
 *
 * Class CacheService
 * @package App\Services
 */
class CacheService extends Cache
{

    /**
     * 设置缓存
     * @param string $key
     * @param string $value
     * @param int $seconds 缓存时间（秒）
     * @return bool
     */
    public static function setCache($key = "", $value = "", $seconds = 0)
    {
        if ($seconds > 0) return self::put($key, $value, $seconds);
        else return self::put($key, $value);
    }

    /**
     * 获取指定缓存
     * @param string $key
     * @return mixed|null
     */
    public static function getCache($key = "")
    {
        if (empty($key)) return null;
        return self::get($key);
    }


    /**
     * 清除缓存，默认清除所有
     * @param string $key
     * @return bool
     */
    public static function clear($key = "")
    {
        // 清除后端缓存
        if ($key) {
            return self::forget($key);
        } else {
            return self::flush();
        }
    }

    /**
     * 更新后台接口路由缓存
     * @return bool
     */
    public static function clearRouteCache()
    {
        return self::clear('routeServiceCache');
    }


    /**
     * 添加标记缓存
     * @param array $tags
     * @param string $key
     * @param string $value
     * @param null $seconds
     * @return bool
     */
    static function setTagsCache($tags = [], $key = "", $value = "", $seconds = null): bool
    {
        return self::tags($tags)->put($key, $value, $seconds);
    }

    /**
     * 获取标记缓存
     * @param array $tags
     * @param string $key
     * @return array|mixed
     */
    static function getTagsCache($tags = [], $key = "")
    {
        return self::tags($tags)->get($key);
    }

    /**
     * 清除指定标记缓存
     * @param $tags
     * @return bool
     */
    static function clearTagsCache($tags): bool
    {
        return self::tags($tags)->flush();
    }

}
